#include "pth.h"

int main(int argc, char const *argv[])
{
    /*// 建立数据库
    sqlite3 *db = NULL;
    // 打开数据库
    if (sqlite3_open("history.db", &db) != SQLITE_OK)
    {
        // 打印错误信息
        fprintf(stderr, "open err:%s\n", sqlite3_errmsg(db));
        return -1;
    }
    printf("open ok\n");
    // 操作
    // 创建表
    char *errmsg = NULL;
    if (sqlite3_exec(db, "create table if not exists stm(time int,key1 int,val1 float,\
    key2 int,val2 float,key3 int,val3 int,key4 int,val4 int,key5 int,val5 int,\
    key6 int,val6 int,key7 int,val7 int,key8 int,val8 int)",
                     NULL, NULL, &errmsg) != SQLITE_OK)
    {
        fprintf(stderr, "create  err:%s\n", sqlite3_errmsg(db));
        fprintf(stderr, "create  err:%s\n", errmsg);
        return -1;
    }
    if (sqlite3_exec(db, "create table if not exists stu(time int,key1 int,val1 int,\
    key2 int,val2 int,key3 int,val3 int,key4 int,val4 int,key5 int,val5 float,\
    key6 int,val6 int)",
                     NULL, NULL, &errmsg) != SQLITE_OK)
    {
        fprintf(stderr, "create  err:%s\n", sqlite3_errmsg(db));
        fprintf(stderr, "create  err:%s\n", errmsg);
        return -1;
    }
*/
    // 反序列化
    FILE *json = fopen("./json.json", "r");
    if (json == NULL)
    {
        perror("open json file err");
        return -1;
    }
    char buf[2048] = "";
    fread(buf, 2048, 1, json);
    fclose(json);

    cJSON *root = cJSON_Parse(buf);

    struct shm_param shm_updata;
    int ret = shm_init(&shm_updata, "updata", 32);
    if (ret < 0)
    {
        printf("fail to shm_updata_init\n");
        return -1;
    }
    shm_updata_addr = shm_getaddr(&shm_updata);
    if (shm_updata_addr == NULL)
    {
        printf("fail to updata_shm_getaddr\n");
        return -1;
    }
    cJSON *item = cJSON_GetObjectItem(root, "version");
    strcpy((char *)shm_updata_addr, item->valuestring);
    printf("升级\n");

    struct shm_param shm_report;
    ret = shm_init(&shm_report, "report", 64);
    if (ret < 0)
    {
        printf("fail to shm_report_init\n");
        return -1;
    }
    shm_report_addr = shm_getaddr(&shm_report);
    if (shm_report_addr == NULL)
    {
        printf("fail to report_shm_getaddr\n");
        return -1;
    }
    item = cJSON_GetObjectItem(root, "report");
    Shm_Report *shm_report_addr_struct = (Shm_Report *)shm_report_addr;
    cJSON *mode = cJSON_GetObjectItem(item, "mode");
    cJSON *period = cJSON_GetObjectItem(item, "period");
    shm_report_addr_struct->mode = mode->valueint;
    shm_report_addr_struct->period = period->valueint;
    printf("上报模式\n");

    struct shm_param shm_mqtt_server;
    ret = shm_init(&shm_mqtt_server, "mqtt_server", 64);
    if (ret < 0)
    {
        printf("fail to shm_mqtt_server_init\n");
        return -1;
    }
    shm_mqtt_server_addr = shm_getaddr(&shm_mqtt_server);
    if (shm_mqtt_server_addr == NULL)
    {
        printf("fail to mqtt_server_shm_getaddr\n");
        return -1;
    }
    item = cJSON_GetObjectItem(root, "mqtt_server");
    Shm_Addr_Port *shm_mqtt_server_addr_struct = (Shm_Addr_Port *)shm_mqtt_server_addr;
    cJSON *addr = cJSON_GetObjectItem(item, "addr");
    cJSON *port = cJSON_GetObjectItem(item, "port");
    strcpy(shm_mqtt_server_addr_struct->addr, addr->valuestring);
    shm_mqtt_server_addr_struct->port = port->valueint;
    printf("mqtt地址端口\n");

    struct shm_param shm_mb_dev;
    ret = shm_init(&shm_mb_dev, "mb_dev", 64);
    if (ret < 0)
    {
        printf("fail to shm_mb_dev_init\n");
        return -1;
    }
    shm_mb_dev_addr = shm_getaddr(&shm_mb_dev);
    if (shm_mb_dev_addr == NULL)
    {
        printf("fail to mb_dev_shm_getaddr\n");
        return -1;
    }
    item = cJSON_GetObjectItem(root, "mb_dev");
    Shm_Addr_Port *shm_mb_dev_addr_struct = (Shm_Addr_Port *)shm_mb_dev_addr;
    addr = cJSON_GetObjectItem(item, "addr");
    port = cJSON_GetObjectItem(item, "port");
    strcpy(shm_mb_dev_addr_struct->addr, addr->valuestring);
    shm_mb_dev_addr_struct->port = port->valueint;
    printf("modbus地址端口\n");

    struct shm_param shm_stm;
    ret = shm_init(&shm_stm, "stm", 1024);
    if (ret < 0)
    {
        printf("fail to shm_stm_init\n");
        return -1;
    }
    printf("innit\n");
    shm_stm_addr = shm_getaddr(&shm_stm);
    if (shm_stm_addr == NULL)
    {
        printf("fail to stm_shm_getaddr\n");
        return -1;
    }
    printf("addr\n");
    item = cJSON_GetObjectItem(root, "m0");
    cJSON *data = cJSON_GetObjectItem(item, "data");
    int stmnum = cJSON_GetArraySize(data);
    int *shm_stm_addr_int = (int *)shm_stm_addr;
    *shm_stm_addr_int = stmnum;
    Shm_Stm *shm_stm_addr_struct = (Shm_Stm *)(shm_stm_addr_int + 1);
    printf("%d\n", stmnum);
    for (size_t i = 0; i < stmnum; i++)
    {
        cJSON *data_object = cJSON_GetArrayItem(data, i);
        cJSON *key = cJSON_GetObjectItem(data_object, "key");
        cJSON *type = cJSON_GetObjectItem(data_object, "type");

        // 存共享内存
        shm_stm_addr_struct->key = key->valueint;
        shm_stm_addr_struct->type = type->valueint;
        shm_stm_addr_struct++;
    }
    printf("stm初始化完成\n");

    // 建立stm共享内存
    struct shm_param shm_modbus;
    ret = shm_init(&shm_modbus, "modbus", 1024);
    if (ret < 0)
    {
        printf("fail to shm_modbus_init\n");
        return -1;
    }
    shm_modbus_addr = shm_getaddr(&shm_modbus);
    if (shm_modbus_addr == NULL)
    {
        printf("fail to modbus_shm_getaddr\n");
        return -1;
    }
    item = cJSON_GetObjectItem(root, "modbus");
    data = cJSON_GetObjectItem(item, "data");
    int modbusnum = cJSON_GetArraySize(data);
    int *shm_modbus_addr_int = (int *)shm_modbus_addr;
    *shm_modbus_addr_int = modbusnum;
    Shm_Modbus *shm_modbus_addr_struct = (Shm_Modbus *)(shm_modbus_addr_int + 1);
    for (size_t i = 0; i < modbusnum; i++)
    {
        cJSON *data_object = cJSON_GetArrayItem(data, i);
        cJSON *key = cJSON_GetObjectItem(data_object, "key");
        cJSON *type = cJSON_GetObjectItem(data_object, "type");
        cJSON *addr = cJSON_GetObjectItem(data_object, "addr");
        // 存共享内存
        shm_modbus_addr_struct->key = key->valueint;
        shm_modbus_addr_struct->type = type->valueint;
        shm_modbus_addr_struct->addr = addr->valueint;
        shm_modbus_addr_struct++;
    }
    printf("modbus共享内存初始化完成\n");

    // 创建线程
    pthread_t tid, tid1, tid2, tid3;
    if (pthread_create(&tid, NULL, pthread_upload, NULL))
    {
        printf("create err\n");
        return -1;
    }

    // if (pthread_create(&tid1, NULL, pthread_sqlite, NULL))
    // {
    //     printf("create err\n");
    //     return -1;
    // }
    if (pthread_create(&tid2, NULL, pthread_control, NULL))
    {
        printf("create err\n");
        return -1;
    }
    if (pthread_create(&tid3, NULL, pthread_yuanyuzhou, NULL))
    {
        printf("create err\n");
        return -1;
    }   
    while(1)
    {
        
        
     
        sleep(1);
    }
    shm_del(&shm_updata);
    shm_del(&shm_report);
    shm_del(&shm_mqtt_server);
    shm_del(&shm_mb_dev);
    shm_del(&shm_stm);
    shm_del(&shm_modbus);

    return 0;
}
